#pragma once
#include "Xps/Protocol/IProtocol.h"
#include "Xps/Object/IObjectFactory.h"

namespace Xps
{
    namespace TCP
    {
        class Client :
            public IProtocol
        {
        public:
            Client(const std::string &_Name, const asio::ip::address &_Address, const short _Port, const IObjectFactory *_ObjectFactory);
            ~Client(void);

            virtual const std::string & getName() const;
            virtual void Start();
            virtual void Stop();
            virtual void Send(IPacket::Ptr _Packet);

        private:
            void Initialize();
            void Connect(const asio::ip::address &_Address, const short _Port);
            void Recieve();
            void Sender();
            void ProcessPackets();

            std::string name;
            bool is_running;
            asio::ip::address address;
            short port;
            boost::thread m_connector;
            boost::thread m_reciever;
            boost::thread th_sender;
            boost::thread th_process_packets;
            boost::shared_ptr<asio::io_service> io_service;
            boost::shared_ptr<tcp::socket> socket;
            concurrent_out_queue outgoing_queue;
            concurrent_in_queue incomming_queue;
            HANDLE recieve_event;
            HANDLE sender_event;
            const IObjectFactory *object_factory;
        };
    };
};